<?php

namespace app\wmj\controller;

use support\Request;
use app\controller\WmjBaseController;
class SwitchController
{
    protected $noNeedLogin = ['powerOn','powerOff','getpowerinfo','delayturnoff'];
    public function index(Request $request)
    {
        return response(__CLASS__);
    }
    //通电
    public function powerOn(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "data" => [
                "cmd_type" => "turnon",
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //断电
    public function powerOff(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "data" => [
                "cmd_type" => "turnoff",
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //查询设备信息（功率、电流、电压等相关信息）
    public function getpowerinfo(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "data" => [
                "cmd_type" => "getdevinfo",
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //延时关电（71.0.33及以上版本支持）
    public function delayturnoff(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $delaytime = $request->post('delaytime','');
        if(!$delaytime){
            return json(['code'=>0,'msg'=>'缺失参数delaytime']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "data" => [
                "cmd_type" => "delayturnoff",
                "info"=>[
                    "delaytime"=>(int)$delaytime
                ]
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //延时开电（71.0.33及以上版本支持）
    public function delayturnon(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $delaytime = $request->post('delaytime','');
        if(!$delaytime){
            return json(['code'=>0,'msg'=>'缺失参数delaytime']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "data" => [
                "cmd_type" => "delayturnon",
                "info"=>[
                    "delaytime"=>(int)$delaytime
                ]
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //注册绑定
    public function regdev($device_sn,$app_id,$app_secret)
    {
        $WmjBase = new WmjBaseController();
        $res = $WmjBase->Request("register", ["device_sn" => $device_sn],$app_id,$app_secret);
        return $res;
    }

}
